<?php
class Administrator_ProductController extends Nguyen_Controller_Action{
	public function init(){
		$this->loadTemplate('administrator');
	}
	public function indexAction(){ 
		print_r($this->_request->getParams());
		$auth = Zend_Auth::getInstance();
		echo '<pre>';
		$info = $auth->getIdentity();
		print_r($info);
		echo '</pre>';
	}
	public function productAction(){ 
		$model = new Administrator_Model_Product_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/product/product?task=new');
			}
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');	
				$store = $this->_request->getParam('store','0');	
				$type = $this->_request->getParam('type','0');	
				$cat = $this->_request->getParam('cat','0');	
				$manufact = $this->_request->getParam('manufact','');	
				$price = $this->_request->getParam('price','0');	
				$currency = $this->_request->getParam('curency','');	
				$salenb = $this->_request->getParam('salenb','0');	
				$salepc = $this->_request->getParam('salepc','0');	
				$tag = $this->_request->getParam('tag','');			
				$intro = $this->_request->getParam('intro','');
				$attribute = $this->_request->getParam('attribute','');
				$warranty = $this->_request->getParam('warranty','');
				if($task == 'new'){
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];					
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/'.$imgname)){
						
					}
					else{
						$imgname = '';
					}
					$a = $model->insertPro_details($name, $imgname, $cat, $type, $store, $manufact, $price, $currency, $salenb, $salepc, $intro, $attribute, $warranty, $model->curentday(), '1');
					$arrtag = explode(",", $tag);
					foreach ($arrtag as $value) {
						$tagid = '';
						if($model->checkExistTag($name)== false) $tagid = $model->insertTag($value);
						else $tagid = $model->GetTagIdByName($value);
						$model->insertContentTag($a, $tagid);		
					}
				}
				if($task == 'edit'){
					$info =$model->GetProductInfo($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/'.$imgname)){
						$model->DeleteImageOnHost($info['image'], 'product');
					}
					else{
						$imgname = $info['image'];
					}
					$model->updatePro_details($id, $name, $imgname, $cat, $type, $store, $manufact, $price, $currency, $salenb, $salepc, $intro, $attribute, $warranty);
					$model->deleteContentTagByContent($id);
					$arrtag = explode(",", $tag); 
					foreach ($arrtag as $value) {
						$tagid = '';
						if($model->checkExistTag($value)==false)$tagid = $model->insertTag($value);
						else $tagid = $model->GetTagIdByName($value);
						$model->insertContentTag($id, $tagid);			
					}
				}
				$this->_redirect('/administrator/product/product');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllProduct() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/product/product?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				$i = 0;
				foreach ($model->GetAllProduct() as $value) {
					if($this->_request->getParam($value['id'])){
						$model->deleteProduct($value['id']);
						$i++;
					}
					if($i>0)$this->view->msg = 'Xoá thành công '.$i.' sản phẩm';
				}
				$this->_redirect('/administrator/product/product');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/product/product');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Quản lý sản phẩm',true);
	}
	public function producttypeAction(){ 
		$model = new Administrator_Model_Product_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/product/producttype?task=new');
			}
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');	
				$store = $this->_request->getParam('store','');				
				if($task == 'new'){
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];					
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/type/'.$imgname)){
						
					}
					else{
						$imgname = '';
					}
					$model->insertPro_type($name, $imgname,$store, '1');
				}
				if($task == 'edit'){
					$info =$model->GetProduct_type($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/type/'.$imgname)){
						$model->DeleteImageOnHost($info['image'], 'product/type');
					}
					else{
						$imgname = $info['image'];
					}
					$model->updatePro_type($id, $name, $imgname,$store);
					
				}
				$this->_redirect('/administrator/product/producttype');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllProduct_type() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/product/producttype?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				foreach ($model->GetAllProduct_type() as $value) {
					if($this->_request->getParam($value['id'])){
						$count = $model->CountCatInType($value['id']);
						if($count == 0){
							$model->delPro_type($value['id']);
							$this->_redirect('/administrator/product/producttype ');
						}
						else {
							$this->view->msg = 'Vẫn còn danh mục sản phẩm thuộc loại, không thể xóa';
						}
					}
				}
				$this->_redirect('/administrator/product/producttype ');
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/product/producttype');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Quản lý loại sản phẩm',true);
	}
public function productmanufactAction(){
		$model = new Administrator_Model_Product_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/product/productmanufact?task=new');
			}
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');
				$website = $this->_request->getParam('website','');
				
				if($task == 'new'){
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/manufact/'.$imgname)){
	
					}
					else{
						$imgname = '';
					}
					$model->insertPro_manufact($name, $imgname, $website, '1');
				}
				if($task == 'edit'){
					$info =$model->GetProduct_manufactInfo($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/manufact/'.$imgname)){
						$model->DeleteImageOnHost($info['image'], 'product/manufact');
					}
					else{
						$imgname = $info['image'];
					}
					$model->updatePro_manufact($id, $name, $imgname, $website, '1');
						
				}
				$this->_redirect('/administrator/product/productmanufact');
			}
				
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllProduct_manufact() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/product/productmanufact?task=edit&id='.$value['id']);
				}
			}
				
			if ($this->_request->getParam('btdelete')){
				foreach ($model->GetAllProduct_manufact() as $value) {
					if($this->_request->getParam($value['id'])){
						$count = $model->CountProInManufact($value['id']);
						if($count == 0){
							$model->delPro_manufact($value['id']);
							$this->_redirect('/administrator/product/productmanufact ');
						}
						else {
							$this->view->msg = 'Vẫn còn sản phẩm thuộc nhà sản xuất này, không thể xóa';
						}
					}
				}
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/product/productmanufact');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Quản lý nhà sản xuất',true);
	}
	
	public function currencyAction(){
		$model = new Administrator_Model_Product_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id','');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/product/currency?task=new');
			}
			
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');
				$display = $this->_request->getParam('display','');
				$value = $this->_request->getParam('value','');				
				if($task == 'new'){										
					$model->insertPro_currency($name, $display, $value);
				}
				if($task == 'edit'){				
					$model->updatePro_currency($id, $name, $display, $value);
				}
				$this->_redirect('/administrator/product/currency');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllPro_currency() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/product/currency?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				
				foreach ($model->GetAllPro_currency() as $value) {
					if($this->_request->getParam($value['id']))
					{
						$count = $model->CountCurencyInStore($value['id']);
						if($count == 0){
							$model->delPro_currency($value['id']);
							$this->_redirect('/administrator/product/currency ');
						}
					else {
							$this->view->msg = 'Có cửa hàng đang sử dụng loại tiền tệ này, không thể xóa';
						}
					}
				}
				
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/product/currency');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin thể loại',true);
	}
	
	public function storeAction(){
		$model = new Administrator_Model_Product_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id','');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/product/store?task=new');
			}
			
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');					
				$address = $this->_request->getParam('address','');
				$email = $this->_request->getParam('email','');
				$phone = $this->_request->getParam('phone','');
				$currency = $this->_request->getParam('currency','');
				$intro = $this->_request->getParam('intro','');			
				if($task == 'new'){
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];					
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/store/'.$imgname)){
						
					}
					else{
						$imgname = '';
					}
					$model->insertPro_store($name, $imgname, $address, $phone, $email, $currency, $intro, '1');
				}
				if($task == 'edit'){
					$info =$model->GetPro_storeInfo($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/store/'.$imgname)){
						$model->DeleteImageOnHost($info['image'], 'product/store');
					}
					else{
						$imgname = $info['image'];
					}
					
					$model->updatePro_store($id, $name, $imgname, $address, $phone, $email, $currency, $intro);
				}
				$this->_redirect('/administrator/product/store');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllPro_store() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/product/store?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				
				foreach ($model->GetAllPro_store() as $value) {
					if($this->_request->getParam($value['id']))
					{
						$count = $model->CountProInStore($value['id']);
						if($count == 0){
							if($model->CountTypeInStore($value['id'])== 0){
								$model->deleteStore($value['id']);
								$this->_redirect('/administrator/product/store ');
							}
							else $this->view->msg = 'Trong cửa hàng vẫn còn loại sản phẩm, không thể xóa';
						}else {
							$this->view->msg = 'Trong cửa hàng vẫn còn sản phẩm, không thể xóa';
						}
					}
				}
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/product/store');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin thể loại',true);
	}

	public function catAction(){
		$model = new Administrator_Model_Product_View();
		$task = $this->_request->getParam('task','list');
		$id = $this->_request->getParam('id','');
		$this->view->task = $task;
		$this->view->id = $id;
		if ($this->_request->isPost()){	
			if ($this->_request->getParam('btadd')){
				$this->_redirect('/administrator/product/cat?task=new');
			}
			
			if ($this->_request->getParam('btsave')){
				$name = $this->_request->getParam('name','');		
				$type = $this->_request->getParam('type','');	
				if($task == 'new'){										
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];					
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/cat/'.$imgname)){
						
					}
					else{
						$imgname = '';
					}
					
					$model->insertPro_cat($name, $imgname, $type, '1');
				}
				if($task == 'edit'){				
					$info =$model->GetProduct_type($id);
					$f = $_FILES['image'];
					$file_temp = $f['tmp_name'];
					$filename = $f['name'];
					$imgname = md5($model->curentday()).$filename;
					if(move_uploaded_file($file_temp, PUBLIC_PATH.'/images/product/cat/'.$imgname)){
						$model->DeleteImageOnHost($info['image'], 'product/cat');
					}
					else{
						$imgname = $info['image'];
					}
					$model->updatePro_cat($id, $name, $imgname, $type);
					
				}
				$this->_redirect('/administrator/product/cat');
			}
			
			if ($this->_request->getParam('btedit')){
				foreach ($model->GetAllPro_cat() as $value) {
					if($this->_request->getParam($value['id']))
						$this->_redirect('/administrator/product/cat?task=edit&id='.$value['id']);
				}
			}
			
			if ($this->_request->getParam('btdelete')){
				
				foreach ($model->GetAllPro_cat() as $value) {
					if($this->_request->getParam($value['id']))
					{
						$count = $model->CountProInCat($value['id']);
						if($count == 0){
							$model->deleteCat($value['id']);
							$this->_redirect('/administrator/product/cat');
						}else {
							$this->view->msg = 'Trong danh mục này vẫn còn sản phẩm, không thể xóa';
						}
					}
				}
			}
			if ($this->_request->getParam('btback')){
				$this->_redirect('/administrator/product/cat');
			}
			if ($this->_request->getParam('btoption')){
				$this->_redirect('/administrator');
			}
		}
		$this->view->headTitle('Control Panel | Thông tin thể loại',true);
	}
	public function rendertypebystoreAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_Product_View();
		$store = $this->_request->getParam('storeid');
		foreach ($mview->GetAllProduct_typeByStore($store) as $value) {
			echo '<option value="'.$value['id'].'">'.$value['name'].'</option>';;
		}
	}
	public function rendercatbytypeAction(){
		$this->_helper->layout()->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(true);
        $mview = new Administrator_Model_Product_View();
		$store = $this->_request->getParam('typeid');
		foreach ($mview->GetAllPro_catByTye($store) as $value) {
			echo '<option value="'.$value['id'].'">'.$value['name'].'</option>';;
		}
	}
}